package org.afk.sisy.io;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;

import org.afk.tools.log.*;

public class SisyFileVisitor implements FileVisitor<Path>
{

	private final Side side;
	private final SisyMatcher sisyFileMatcher;
	private final ErrorHandler errorHandler;

	public SisyFileVisitor(final Side side, final SisyMatcher sisyFileMatcher, final ErrorHandler errorHandler)
	{
		this.side = side;
		this.sisyFileMatcher = sisyFileMatcher;
		this.errorHandler = errorHandler;
	}

	@Override
	public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException
	{
		if (exc != null)
			errorHandler.warning("While processing " + dir, exc);
		return FileVisitResult.CONTINUE;
	}

	@Override
	public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException
	{
		sisyFileMatcher.nextFile(side, dir);

		return FileVisitResult.CONTINUE;
	}

	@Override
	public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException
	{
		sisyFileMatcher.nextFile(side, file);
		return FileVisitResult.CONTINUE;
	}

	@Override
	public FileVisitResult visitFileFailed(final Path file, final IOException exc) throws IOException
	{
		if (exc != null)
			errorHandler.warning("While processing " + file, exc);
		return FileVisitResult.CONTINUE;
	}

}
